HTMLify
Longest Consecutive Elements.cpp
Views: 1 | Author: cody
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | int longestSuccessiveElements(vector<int>&a) { // Write your code here. if(a.size() == 0) return 0; sort(a.begin(), a.end()); int n = a.size(); int lastsmaller = INT_MIN; int cnt = 0; int longest = 1; for(int i = 0; i<n; i++){ if(a[i] -1 == lastsmaller){ cnt = cnt + 1; lastsmaller = a[i]; } else if(lastsmaller != a[i]){ cnt = 1; lastsmaller = a[i]; } longest = max(longest,cnt); } return longest; } |